/*
Navicat MySQL Data Transfer

Source Server         : localhost
Source Server Version : 50728
Source Host           : localhost:3306
Source Database       : orleans_demo

Target Server Type    : MYSQL
Target Server Version : 50728
File Encoding         : 65001

Date: 2023-07-19 15:38:32
*/

SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `orleansmembershiptable`
-- ----------------------------
DROP TABLE IF EXISTS `orleansmembershiptable`;
CREATE TABLE `orleansmembershiptable` (
  `DeploymentId` varchar(150) CHARACTER SET utf8 NOT NULL,
  `Address` varchar(45) NOT NULL,
  `Port` int(11) NOT NULL,
  `Generation` int(11) NOT NULL,
  `SiloName` varchar(150) CHARACTER SET utf8 NOT NULL,
  `HostName` varchar(150) CHARACTER SET utf8 NOT NULL,
  `Status` int(11) NOT NULL,
  `ProxyPort` int(11) DEFAULT NULL,
  `SuspectTimes` varchar(8000) DEFAULT NULL,
  `StartTime` datetime NOT NULL,
  `IAmAliveTime` datetime NOT NULL,
  PRIMARY KEY (`DeploymentId`,`Address`,`Port`,`Generation`),
  CONSTRAINT `FK_MembershipTable_MembershipVersionTable_DeploymentId` FOREIGN KEY (`DeploymentId`) REFERENCES `orleansmembershipversiontable` (`DeploymentId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ----------------------------
-- Records of orleansmembershiptable
-- ----------------------------
BEGIN;
COMMIT;

-- ----------------------------
-- Table structure for `orleansmembershipversiontable`
-- ----------------------------
DROP TABLE IF EXISTS `orleansmembershipversiontable`;
CREATE TABLE `orleansmembershipversiontable` (
  `DeploymentId` varchar(150) CHARACTER SET utf8 NOT NULL,
  `Timestamp` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `Version` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`DeploymentId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ----------------------------
-- Records of orleansmembershipversiontable
-- ----------------------------
BEGIN;
COMMIT;

-- ----------------------------
-- Table structure for `orleansquery`
-- ----------------------------
DROP TABLE IF EXISTS `orleansquery`;
CREATE TABLE `orleansquery` (
  `QueryKey` varchar(64) NOT NULL,
  `QueryText` varchar(8000) NOT NULL,
  PRIMARY KEY (`QueryKey`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ----------------------------
-- Records of orleansquery
-- ----------------------------
BEGIN;
INSERT INTO `orleansquery` VALUES ('CleanupDefunctSiloEntriesKey', ''), ('ClearStorageKey', '\r\n    call ClearStorage(@GrainIdHash, @GrainIdN0, @GrainIdN1, @GrainTypeHash, @GrainTypeString, @GrainIdExtensionString, @ServiceId, @GrainStateVersion);'), ('DeleteMembershipTableEntriesKey', '\r\n    DELETE FROM OrleansMembershipTable\r\n    WHERE DeploymentId = @DeploymentId AND @DeploymentId IS NOT NULL;\r\n    DELETE FROM OrleansMembershipVersionTable\r\n    WHERE DeploymentId = @DeploymentId AND @DeploymentId IS NOT NULL;\r\n'), ('DeleteReminderRowKey', '\r\n    DELETE FROM OrleansRemindersTable\r\n    WHERE\r\n        ServiceId = @ServiceId AND @ServiceId IS NOT NULL\r\n        AND GrainId = @GrainId AND @GrainId IS NOT NULL\r\n        AND ReminderName = @ReminderName AND @ReminderName IS NOT NULL\r\n        AND Version = @Version AND @Version IS NOT NULL;\r\n    SELECT ROW_COUNT();\r\n'), ('DeleteReminderRowsKey', '\r\n    DELETE FROM OrleansRemindersTable\r\n    WHERE\r\n        ServiceId = @ServiceId AND @ServiceId IS NOT NULL;\r\n'), ('GatewaysQueryKey', '\r\n    SELECT\r\n        Address,\r\n        ProxyPort,\r\n        Generation\r\n    FROM\r\n        OrleansMembershipTable\r\n    WHERE\r\n        DeploymentId = @DeploymentId AND @DeploymentId IS NOT NULL\r\n        AND Status = @Status AND @Status IS NOT NULL\r\n        AND ProxyPort > 0;\r\n'), ('InsertMembershipKey', '\r\n    call InsertMembershipKey(@DeploymentId, @Address, @Port, @Generation,\r\n    @Version, @SiloName, @HostName, @Status, @ProxyPort, @StartTime, @IAmAliveTime);'), ('InsertMembershipVersionKey', '\r\n    INSERT INTO OrleansMembershipVersionTable\r\n    (\r\n        DeploymentId\r\n    )\r\n    SELECT * FROM ( SELECT @DeploymentId ) AS TMP\r\n    WHERE NOT EXISTS\r\n    (\r\n    SELECT 1\r\n    FROM\r\n        OrleansMembershipVersionTable\r\n    WHERE\r\n        DeploymentId = @DeploymentId AND @DeploymentId IS NOT NULL\r\n    );\r\n\r\n    SELECT ROW_COUNT();\r\n'), ('MembershipReadAllKey', '\r\n    SELECT\r\n        v.DeploymentId,\r\n        m.Address,\r\n        m.Port,\r\n        m.Generation,\r\n        m.SiloName,\r\n        m.HostName,\r\n        m.Status,\r\n        m.ProxyPort,\r\n        m.SuspectTimes,\r\n        m.StartTime,\r\n        m.IAmAliveTime,\r\n        v.Version\r\n    FROM\r\n        OrleansMembershipVersionTable v LEFT OUTER JOIN OrleansMembershipTable m\r\n        ON v.DeploymentId = m.DeploymentId\r\n    WHERE\r\n        v.DeploymentId = @DeploymentId AND @DeploymentId IS NOT NULL;\r\n'), ('MembershipReadRowKey', '\r\n    SELECT\r\n        v.DeploymentId,\r\n        m.Address,\r\n        m.Port,\r\n        m.Generation,\r\n        m.SiloName,\r\n        m.HostName,\r\n        m.Status,\r\n        m.ProxyPort,\r\n        m.SuspectTimes,\r\n        m.StartTime,\r\n        m.IAmAliveTime,\r\n        v.Version\r\n    FROM\r\n        OrleansMembershipVersionTable v\r\n        -- This ensures the version table will returned even if there is no matching membership row.\r\n        LEFT OUTER JOIN OrleansMembershipTable m ON v.DeploymentId = m.DeploymentId\r\n        AND Address = @Address AND @Address IS NOT NULL\r\n        AND Port = @Port AND @Port IS NOT NULL\r\n        AND Generation = @Generation AND @Generation IS NOT NULL\r\n    WHERE\r\n        v.DeploymentId = @DeploymentId AND @DeploymentId IS NOT NULL;\r\n'), ('ReadFromStorageKey', 'SELECT\r\n        PayloadBinary,\r\n        UTC_TIMESTAMP(),\r\n        Version\r\n    FROM\r\n        OrleansStorage\r\n    WHERE\r\n        GrainIdHash = @GrainIdHash\r\n        AND GrainTypeHash = @GrainTypeHash AND @GrainTypeHash IS NOT NULL\r\n        AND GrainIdN0 = @GrainIdN0 AND @GrainIdN0 IS NOT NULL\r\n        AND GrainIdN1 = @GrainIdN1 AND @GrainIdN1 IS NOT NULL\r\n        AND GrainTypeString = @GrainTypeString AND GrainTypeString IS NOT NULL\r\n        AND ((@GrainIdExtensionString IS NOT NULL AND GrainIdExtensionString IS NOT NULL AND GrainIdExtensionString = @GrainIdExtensionString) OR @GrainIdExtensionString IS NULL AND GrainIdExtensionString IS NULL)\r\n        AND ServiceId = @ServiceId AND @ServiceId IS NOT NULL\r\n        LIMIT 1;'), ('ReadRangeRows1Key', '\r\n    SELECT\r\n        GrainId,\r\n        ReminderName,\r\n        StartTime,\r\n        Period,\r\n        Version\r\n    FROM OrleansRemindersTable\r\n    WHERE\r\n        ServiceId = @ServiceId AND @ServiceId IS NOT NULL\r\n        AND GrainHash > @BeginHash AND @BeginHash IS NOT NULL\r\n        AND GrainHash <= @EndHash AND @EndHash IS NOT NULL;\r\n'), ('ReadRangeRows2Key', '\r\n    SELECT\r\n        GrainId,\r\n        ReminderName,\r\n        StartTime,\r\n        Period,\r\n        Version\r\n    FROM OrleansRemindersTable\r\n    WHERE\r\n        ServiceId = @ServiceId AND @ServiceId IS NOT NULL\r\n        AND ((GrainHash > @BeginHash AND @BeginHash IS NOT NULL)\r\n        OR (GrainHash <= @EndHash AND @EndHash IS NOT NULL));\r\n'), ('ReadReminderRowKey', '\r\n    SELECT\r\n        GrainId,\r\n        ReminderName,\r\n        StartTime,\r\n        Period,\r\n        Version\r\n    FROM OrleansRemindersTable\r\n    WHERE\r\n        ServiceId = @ServiceId AND @ServiceId IS NOT NULL\r\n        AND GrainId = @GrainId AND @GrainId IS NOT NULL\r\n        AND ReminderName = @ReminderName AND @ReminderName IS NOT NULL;\r\n'), ('ReadReminderRowsKey', '\r\n    SELECT\r\n        GrainId,\r\n        ReminderName,\r\n        StartTime,\r\n        Period,\r\n        Version\r\n    FROM OrleansRemindersTable\r\n    WHERE\r\n        ServiceId = @ServiceId AND @ServiceId IS NOT NULL\r\n        AND GrainId = @GrainId AND @GrainId IS NOT NULL;\r\n'), ('UpdateIAmAlivetimeKey', '\r\n    -- This is expected to never fail by Orleans, so return value\r\n    -- is not needed nor is it checked.\r\n    UPDATE OrleansMembershipTable\r\n    SET\r\n        IAmAliveTime = @IAmAliveTime\r\n    WHERE\r\n        DeploymentId = @DeploymentId AND @DeploymentId IS NOT NULL\r\n        AND Address = @Address AND @Address IS NOT NULL\r\n        AND Port = @Port AND @Port IS NOT NULL\r\n        AND Generation = @Generation AND @Generation IS NOT NULL;\r\n'), ('UpdateMembershipKey', '\r\n    START TRANSACTION;\r\n\r\n    UPDATE OrleansMembershipVersionTable\r\n    SET\r\n        Version = Version + 1\r\n    WHERE\r\n        DeploymentId = @DeploymentId AND @DeploymentId IS NOT NULL\r\n        AND Version = @Version AND @Version IS NOT NULL;\r\n\r\n    UPDATE OrleansMembershipTable\r\n    SET\r\n        Status = @Status,\r\n        SuspectTimes = @SuspectTimes,\r\n        IAmAliveTime = @IAmAliveTime\r\n    WHERE\r\n        DeploymentId = @DeploymentId AND @DeploymentId IS NOT NULL\r\n        AND Address = @Address AND @Address IS NOT NULL\r\n        AND Port = @Port AND @Port IS NOT NULL\r\n        AND Generation = @Generation AND @Generation IS NOT NULL\r\n        AND ROW_COUNT() > 0;\r\n\r\n    SELECT ROW_COUNT();\r\n    COMMIT;\r\n'), ('UpsertReminderRowKey', '\r\n    INSERT INTO OrleansRemindersTable\r\n    (\r\n        ServiceId,\r\n        GrainId,\r\n        ReminderName,\r\n        StartTime,\r\n        Period,\r\n        GrainHash,\r\n        Version\r\n    )\r\n    VALUES\r\n    (\r\n        @ServiceId,\r\n        @GrainId,\r\n        @ReminderName,\r\n        @StartTime,\r\n        @Period,\r\n        @GrainHash,\r\n        last_insert_id(0)\r\n    )\r\n    ON DUPLICATE KEY\r\n    UPDATE\r\n        StartTime = @StartTime,\r\n        Period = @Period,\r\n        GrainHash = @GrainHash,\r\n        Version = last_insert_id(Version+1);\r\n\r\n\r\n    SELECT last_insert_id() AS Version;\r\n'), ('WriteToStorageKey', '\r\n    call WriteToStorage(@GrainIdHash, @GrainIdN0, @GrainIdN1, @GrainTypeHash, @GrainTypeString, @GrainIdExtensionString, @ServiceId, @GrainStateVersion, @PayloadBinary);');
COMMIT;

-- ----------------------------
-- Table structure for `orleansreminderstable`
-- ----------------------------
DROP TABLE IF EXISTS `orleansreminderstable`;
CREATE TABLE `orleansreminderstable` (
  `ServiceId` varchar(150) CHARACTER SET utf8 NOT NULL,
  `GrainId` varchar(150) NOT NULL,
  `ReminderName` varchar(150) CHARACTER SET utf8 NOT NULL,
  `StartTime` datetime NOT NULL,
  `Period` bigint(20) NOT NULL,
  `GrainHash` int(11) NOT NULL,
  `Version` int(11) NOT NULL,
  PRIMARY KEY (`ServiceId`,`GrainId`,`ReminderName`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ----------------------------
-- Records of orleansreminderstable
-- ----------------------------
BEGIN;
COMMIT;

-- ----------------------------
-- Table structure for `orleansstorage`
-- ----------------------------
DROP TABLE IF EXISTS `orleansstorage`;
CREATE TABLE `orleansstorage` (
  `GrainIdHash` int(11) NOT NULL,
  `GrainIdN0` bigint(20) NOT NULL,
  `GrainIdN1` bigint(20) NOT NULL,
  `GrainTypeHash` int(11) NOT NULL,
  `GrainTypeString` varchar(512) CHARACTER SET utf8 NOT NULL,
  `GrainIdExtensionString` varchar(512) CHARACTER SET utf8 DEFAULT NULL,
  `ServiceId` varchar(150) CHARACTER SET utf8 NOT NULL,
  `PayloadBinary` blob,
  `ModifiedOn` datetime NOT NULL,
  `Version` int(11) DEFAULT NULL,
  KEY `IX_OrleansStorage` (`GrainIdHash`,`GrainTypeHash`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=16;

-- ----------------------------
-- Records of orleansstorage
-- ----------------------------
BEGIN;
COMMIT;

-- ----------------------------
-- Procedure structure for `ClearStorage`
-- ----------------------------
DROP PROCEDURE IF EXISTS `ClearStorage`;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `ClearStorage`(
    in _GrainIdHash INT,
    in _GrainIdN0 BIGINT,
    in _GrainIdN1 BIGINT,
    in _GrainTypeHash INT,
    in _GrainTypeString NVARCHAR(512),
    in _GrainIdExtensionString NVARCHAR(512),
    in _ServiceId NVARCHAR(150),
    in _GrainStateVersion INT
)
BEGIN
    DECLARE _newGrainStateVersion INT;
    DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN ROLLBACK; RESIGNAL; END;
    DECLARE EXIT HANDLER FOR SQLWARNING BEGIN ROLLBACK; RESIGNAL; END;

    SET _newGrainStateVersion = _GrainStateVersion;

    -- Default level is REPEATABLE READ and may cause Gap Lock issues
    SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
    START TRANSACTION;
    UPDATE OrleansStorage
    SET
        PayloadBinary = NULL,
        Version = Version + 1
    WHERE
        GrainIdHash = _GrainIdHash AND _GrainIdHash IS NOT NULL
        AND GrainTypeHash = _GrainTypeHash AND _GrainTypeHash IS NOT NULL
        AND GrainIdN0 = _GrainIdN0 AND _GrainIdN0 IS NOT NULL
        AND GrainIdN1 = _GrainIdN1 AND _GrainIdN1 IS NOT NULL
        AND GrainTypeString = _GrainTypeString AND _GrainTypeString IS NOT NULL
        AND ((_GrainIdExtensionString IS NOT NULL AND GrainIdExtensionString IS NOT NULL AND GrainIdExtensionString = _GrainIdExtensionString) OR _GrainIdExtensionString IS NULL AND GrainIdExtensionString IS NULL)
        AND ServiceId = _ServiceId AND _ServiceId IS NOT NULL
        AND Version IS NOT NULL AND Version = _GrainStateVersion AND _GrainStateVersion IS NOT NULL
        LIMIT 1;

    IF ROW_COUNT() > 0
    THEN
        SET _newGrainStateVersion = _GrainStateVersion + 1;
    END IF;

    SELECT _newGrainStateVersion AS NewGrainStateVersion;
    COMMIT;
END
;;
DELIMITER ;

-- ----------------------------
-- Procedure structure for `InsertMembershipKey`
-- ----------------------------
DROP PROCEDURE IF EXISTS `InsertMembershipKey`;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `InsertMembershipKey`(
    in    _DeploymentId NVARCHAR(150),
    in    _Address VARCHAR(45),
    in    _Port INT,
    in    _Generation INT,
    in    _Version INT,
    in    _SiloName NVARCHAR(150),
    in    _HostName NVARCHAR(150),
    in    _Status INT,
    in    _ProxyPort INT,
    in    _StartTime DATETIME,
    in    _IAmAliveTime DATETIME
)
BEGIN
    DECLARE _ROWCOUNT INT;
    START TRANSACTION;
    INSERT INTO OrleansMembershipTable
    (
        DeploymentId,
        Address,
        Port,
        Generation,
        SiloName,
        HostName,
        Status,
        ProxyPort,
        StartTime,
        IAmAliveTime
    )
    SELECT * FROM ( SELECT
        _DeploymentId,
        _Address,
        _Port,
        _Generation,
        _SiloName,
        _HostName,
        _Status,
        _ProxyPort,
        _StartTime,
        _IAmAliveTime) AS TMP
    WHERE NOT EXISTS
    (
    SELECT 1
    FROM
        OrleansMembershipTable
    WHERE
        DeploymentId = _DeploymentId AND _DeploymentId IS NOT NULL
        AND Address = _Address AND _Address IS NOT NULL
        AND Port = _Port AND _Port IS NOT NULL
        AND Generation = _Generation AND _Generation IS NOT NULL
    );

    UPDATE OrleansMembershipVersionTable
    SET
        Version = Version + 1
    WHERE
        DeploymentId = _DeploymentId AND _DeploymentId IS NOT NULL
        AND Version = _Version AND _Version IS NOT NULL
        AND ROW_COUNT() > 0;

    SET _ROWCOUNT = ROW_COUNT();

    IF _ROWCOUNT = 0
    THEN
        ROLLBACK;
    ELSE
        COMMIT;
    END IF;
    SELECT _ROWCOUNT;
END
;;
DELIMITER ;

-- ----------------------------
-- Procedure structure for `WriteToStorage`
-- ----------------------------
DROP PROCEDURE IF EXISTS `WriteToStorage`;
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `WriteToStorage`(
    in _GrainIdHash INT,
    in _GrainIdN0 BIGINT,
    in _GrainIdN1 BIGINT,
    in _GrainTypeHash INT,
    in _GrainTypeString NVARCHAR(512),
    in _GrainIdExtensionString NVARCHAR(512),
    in _ServiceId NVARCHAR(150),
    in _GrainStateVersion INT,
    in _PayloadBinary BLOB
)
BEGIN
    DECLARE _newGrainStateVersion INT;
    DECLARE _rowCount INT;
    DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN ROLLBACK; RESIGNAL; END;
    DECLARE EXIT HANDLER FOR SQLWARNING BEGIN ROLLBACK; RESIGNAL; END;

    SET _newGrainStateVersion = _GrainStateVersion;

    -- Default level is REPEATABLE READ and may cause Gap Lock issues
    SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
    START TRANSACTION;

    -- Grain state is not null, so the state must have been read from the storage before.
    -- Let's try to update it.
    --
    -- When Orleans is running in normal, non-split state, there will
    -- be only one grain with the given ID and type combination only. This
    -- grain saves states mostly serially if Orleans guarantees are upheld. Even
    -- if not, the updates should work correctly due to version number.
    --
    -- In split brain situations there can be a situation where there are two or more
    -- grains with the given ID and type combination. When they try to INSERT
    -- concurrently, the table needs to be locked pessimistically before one of
    -- the grains gets @GrainStateVersion = 1 in return and the other grains will fail
    -- to update storage. The following arrangement is made to reduce locking in normal operation.
    --
    -- If the version number explicitly returned is still the same, Orleans interprets it so the update did not succeed
    -- and throws an InconsistentStateException.
    --
    -- See further information at https://docs.microsoft.com/dotnet/orleans/grains/grain-persistence.
    IF _GrainStateVersion IS NOT NULL
    THEN
        UPDATE OrleansStorage
        SET
            PayloadBinary = _PayloadBinary,
            ModifiedOn = UTC_TIMESTAMP(),
            Version = Version + 1
        WHERE
            GrainIdHash = _GrainIdHash AND _GrainIdHash IS NOT NULL
            AND GrainTypeHash = _GrainTypeHash AND _GrainTypeHash IS NOT NULL
            AND GrainIdN0 = _GrainIdN0 AND _GrainIdN0 IS NOT NULL
            AND GrainIdN1 = _GrainIdN1 AND _GrainIdN1 IS NOT NULL
            AND GrainTypeString = _GrainTypeString AND _GrainTypeString IS NOT NULL
            AND ((_GrainIdExtensionString IS NOT NULL AND GrainIdExtensionString IS NOT NULL AND GrainIdExtensionString = _GrainIdExtensionString) OR _GrainIdExtensionString IS NULL AND GrainIdExtensionString IS NULL)
            AND ServiceId = _ServiceId AND _ServiceId IS NOT NULL
            AND Version IS NOT NULL AND Version = _GrainStateVersion AND _GrainStateVersion IS NOT NULL
            LIMIT 1;

        IF ROW_COUNT() > 0
        THEN
            SET _newGrainStateVersion = _GrainStateVersion + 1;
            SET _GrainStateVersion = _newGrainStateVersion;
        END IF;
    END IF;

    -- The grain state has not been read. The following locks rather pessimistically
    -- to ensure only on INSERT succeeds.
    IF _GrainStateVersion IS NULL
    THEN
        INSERT INTO OrleansStorage
        (
            GrainIdHash,
            GrainIdN0,
            GrainIdN1,
            GrainTypeHash,
            GrainTypeString,
            GrainIdExtensionString,
            ServiceId,
            PayloadBinary,
            ModifiedOn,
            Version
        )
        SELECT * FROM ( SELECT
            _GrainIdHash,
            _GrainIdN0,
            _GrainIdN1,
            _GrainTypeHash,
            _GrainTypeString,
            _GrainIdExtensionString,
            _ServiceId,
            _PayloadBinary,
            UTC_TIMESTAMP(),
            1) AS TMP
        WHERE NOT EXISTS
        (
            -- There should not be any version of this grain state.
            SELECT 1
            FROM OrleansStorage
            WHERE
                GrainIdHash = _GrainIdHash AND _GrainIdHash IS NOT NULL
                AND GrainTypeHash = _GrainTypeHash AND _GrainTypeHash IS NOT NULL
                AND GrainIdN0 = _GrainIdN0 AND _GrainIdN0 IS NOT NULL
                AND GrainIdN1 = _GrainIdN1 AND _GrainIdN1 IS NOT NULL
                AND GrainTypeString = _GrainTypeString AND _GrainTypeString IS NOT NULL
                AND ((_GrainIdExtensionString IS NOT NULL AND GrainIdExtensionString IS NOT NULL AND GrainIdExtensionString = _GrainIdExtensionString) OR _GrainIdExtensionString IS NULL AND GrainIdExtensionString IS NULL)
                AND ServiceId = _ServiceId AND _ServiceId IS NOT NULL
        ) LIMIT 1;

        IF ROW_COUNT() > 0
        THEN
            SET _newGrainStateVersion = 1;
        END IF;
    END IF;

    SELECT _newGrainStateVersion AS NewGrainStateVersion;
    COMMIT;
END
;;
DELIMITER ;
